package org.dromara.common.web.utils;

import com.alibaba.fastjson.JSON;
import jakarta.servlet.http.HttpServletResponse;
import org.dromara.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * HttpServletResponse帮助类
 */
public final class ResponseUtils {
    public static final Logger log = LoggerFactory
        .getLogger(ResponseUtils.class);


    public static final String STATUS = "code";
    public static final String SUCCESS = "200";
    public static final String ERROR = "500";
    public static final String MESSAGE = "msg";
    public static final String WARN = "warn";
    public static final String DATA = "data";


    /**
     * 发送文本。使用UTF-8编码。
     *
     * @param response HttpServletResponse
     * @param text     发送的字符串
     */
    public static void renderText(HttpServletResponse response, String text) {
        render(response, "text/plain", text);
    }

    /**
     * 发送HTML。使用UTF-8编码。
     *
     * @param response HttpServletResponse
     * @param text     发送的字符串
     */
    public static void renderHtml(HttpServletResponse response, String text) {
        render(response, "text/html", text);
    }

    /**
     * 发送json。使用UTF-8编码。
     *
     * @param response HttpServletResponse
     * @param text     发送的字符串
     */
    public static void renderJson(HttpServletResponse response, String text) {
//        render(response, "application/json", text);
        render(response, "text/plain", text);
    }

    /**
     * 发送xml。使用UTF-8编码。
     *
     * @param response HttpServletResponse
     * @param text     发送的字符串
     */
    public static void renderXml(HttpServletResponse response, String text) {
        render(response, "text/xml", text);
    }

    /**
     * 发送内容。使用UTF-8编码。
     *
     * @param response
     * @param type
     * @param text
     */
    public static void render(HttpServletResponse response, String type,
                              String text) {
        response.setContentType(type + ";charset=UTF-8");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        try {
            if (StringUtils.isNotEmpty(text)) {
                String var = text.length() <= 300 ? text : text.substring(0, 300);
                log.debug("response", var);
            }
            response.getWriter().write(text);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    // 输出JSON成功消息，返回null
    public static void ajaxJsonSuccessMessage(HttpServletResponse response, String message) {

        Map<String, String> jsonMap = new HashMap<String, String>();
        jsonMap.put(STATUS, SUCCESS);
        jsonMap.put(MESSAGE, message);
        renderJson(response, JSON.toJSONString(jsonMap));
    }

    // 输出JSON错误消息，返回null
    public static void ajaxJsonErrorMessage(HttpServletResponse response, String message) {

        Map<String, String> jsonMap = new HashMap<String, String>();
        jsonMap.put(STATUS, ERROR);
        jsonMap.put(MESSAGE, message);
        renderJson(response, JSON.toJSONString(jsonMap));
    }

    // 输出JSON结果成功消息，返回null

    public static void ajaxJsonSuccessResult(HttpServletResponse response, String message, Object data) {

        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put(STATUS, SUCCESS);
        jsonMap.put(MESSAGE, message);
        jsonMap.put(DATA, data);

        String json = JSON.toJSONString(jsonMap);
        renderJson(response, json);
    }

}
